// pages/shoppingCart/shoppingCart.js
Page({
    data: {
        allIn: false, money: 0,
        shopping: [
            {
                id:1,
                title: '现货发顺丰 6期免息',
                name: 'vivo x62 4G 新品拍照',
                num: 1,
                price: 158.00,
                checked: false
            },
            {
                id:2,
                title: '现货发顺丰 6期免息',
                name: 'vivo x60 5G 新品拍照',
                num: 2,
                price: 138.00,
                checked: false
            },
            {
                id:3,
                title: '现货发顺丰 6期免息',
                name: 'vivo x60 5G 新品拍照',
                num: 3,
                price: 3538.22,
                checked: false
            },
            {
                id:4,
                title: '现货发顺丰 6期免息',
                name: 'vivo x60 5G 新品拍照',
                num: 3,
                price: 3538.22,
                checked: false
            },
            {
                id:5,
                title: '现货发顺丰 9期免息',
                name: 'vivo x61 5G 新品拍照',
                num: 3,
                price: 3538.22,
                checked: false
            }
        ],
        cart: [
            {
                img: '',
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    showMoney(){
        console.log(66)
        console.log(this.data.money)
    },
    onLoad: function () {
        const that = this
        let sum = 0
        for (let i = 0; i < that.data.shopping.length; i++) {
            if (that.data.shopping[i].checked) {
                sum += (that.data.shopping[i].price * that.data.shopping[i].num)
            }

        }
        that.setData({
            money: sum
        })
    },
    myevent(e:any){
        const that = this;
        let sum=0;
        const data=that.data.shopping
        const {id,checked}=e.detail.params
        for (let a = 0; a < data.length; a++) {
            if (data[a].id==id){
                data[a].checked=checked
            }
            if (data[a].checked){
                sum+=(data[a].price*data[a].num)
            }
        }
        that.setData({
        money:sum
        })
    },
    handlerName: function () {
        const that = this;
        let sum=0
        for (let i = 0; i < that.data.shopping.length; i++) {
            that.data.shopping[i].checked = (!that.data.allIn)
            if (that.data.shopping[i].checked){
                sum+=(that.data.shopping[i].price*that.data.shopping[i].num)
            }
        }
        that.setData({
            shopping: that.data.shopping,
            allIn: (!that.data.allIn), money:sum
        })
    }
})

